package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.mapper.UserWalletInfoMapper;
import com.cdks.wuxiu.api.model.dto.WalletInfoDTO;
import com.cdks.wuxiu.entity.mysql.UserWalletInfo;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;


/**
 * 钱包信息表 服务实现类
 */
@Slf4j
@Service
public class UserWalletInfoServiceImpl extends ServiceImpl<UserWalletInfoMapper, UserWalletInfo> implements UserWalletInfoService {
    private int expend(long userId, long cost, int costType, int statisType, int expendType, long keepCost) {
        if ((cost == 0) && (keepCost == 0))
        {
            return 0;
        }
        
        int ret  = baseMapper.expend(userId, cost, costType, statisType, expendType, keepCost);
        if (ret == 1)
        {
            return 0;
        }

        log.error("update user expend failed: userId = " + userId + ", cost = " + cost + ", keepCost = " + keepCost + ", costType = " + costType + ", ret = " + ret);

        return -1;
    }

    private int income(long userId, long cost, int costType, int statisType) {
        if (cost == 0)
        {
            return 0;
        }
        
        int ret  = baseMapper.income(userId, cost, costType, statisType);
        if (ret == 1)
        {
            return 0;
        }

        log.error("update user income failed: userId = " + userId + ", cost = " + cost + ", costType = " + costType + ", ret = " + ret);

        return -1;        
    }

    @Override
    public int init(long userId) {
        UserWalletInfo walletInfo = new UserWalletInfo();
        walletInfo.setUserId(userId);

        boolean ret  = super.save(walletInfo);
        if (ret)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public UserWalletInfo getByUserId(long userId) {
        QueryWrapper<UserWalletInfo> wrapper  = new QueryWrapper<>();
        wrapper.lambda().eq(UserWalletInfo::getUserId, userId);
        
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public int decCoin(long userId, long cost, int statisType, int expendType) {
        if (cost == 0)
        {
            return 0;
        }

        int ret  = expend(userId, cost, Constant.COST_TYPE_COIN, statisType, expendType, 0);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public int addCoin(long userId, long cost, int statisType) {
        if (cost == 0)
        {
            return 0;
        }

        int ret  = income(userId, cost, Constant.COST_TYPE_COIN, statisType);
        if (ret < 0)
        {
            return -1;
        }

        return 0;        
    }

    @Override
    public int decBean(long userId, long cost, int statisType, int expendType) {
        if (cost == 0)
        {
            return 0;
        }

        int ret  = expend(userId, cost, Constant.COST_TYPE_BEAN, statisType, expendType, 0);
        if (ret < 0)
        {
            return -1;
        }

        return 0;       
    }

    @Override
    public int addBean(long userId, long cost, int statisType) {
        if (cost == 0)
        {
            return 0;
        }

        int ret  = income(userId, cost, Constant.COST_TYPE_BEAN, statisType);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public WalletInfoDTO getInfoWithScore(long userId, long expire) {
        return baseMapper.getInfoWithScore(userId, expire);
    }
}
